package com.example.gbtdemo.service.impl;

import cn.hutool.core.util.RandomUtil;
import com.example.gbtdemo.mapper.AppUserMapper;
import lombok.extern.slf4j.Slf4j;
import org.springframework.scheduling.annotation.Async;
import org.springframework.scheduling.annotation.AsyncResult;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;
import java.util.concurrent.Future;

/**
 * @ClassName UseraAppBakAsync
 * @Author 刘泽
 * @Mail liuze713@hotmail.com
 * @Date 2023/1/13 12:58
 * @Version 1.0
 */
@Service
@Slf4j
public class UseraAppBakAsync {
    @Resource
    private AppUserMapper appUserMapper;

    @Async(value = "taskliuzeExecutor")
    public Future<Object> addUseraAppBak(Integer page, Integer size) {
        page = page * size;
        Integer integer = appUserMapper.addUseraAppBak(page, size);
        log.info("备份中----线程名:{}-----影响行数:{}", Thread.currentThread().getName(), integer);
        // int c = RandomUtil.randomInt(500, 2000);
        // log.info("备份中----线程睡眠:{}----线程名:{}-----影响行数:{}", c,Thread.currentThread().getName(), integer);
        // try {
        //     Thread.sleep(c);
        // } catch (InterruptedException e) {
        //     throw new RuntimeException(e);
        // }
        return new AsyncResult(null);
    }

    @Async(value = "taskliuzeExecutor")
    public Future<Object> deleteUseraAppBak(Integer size) {
        Integer integer = appUserMapper.DELETEUseraAppBak(size);
        log.info("删除中----线程名:{}-----影响行数:{}", Thread.currentThread().getName(), integer);
        // int c = RandomUtil.randomInt(500, 2000);
        // log.info("删除中----线程睡眠:{}----线程名:{}-----影响行数:{}", c,Thread.currentThread().getName(), integer);
        // try {
        //     Thread.sleep(c);
        // } catch (InterruptedException e) {
        //     throw new RuntimeException(e);
        // }
        return new AsyncResult(null);
    }

    public static void main(String[] args) {
        for (int i = 0; i < 100; i++) {
            int c = RandomUtil.randomInt(500, 5000);
            System.out.println(c);
        }

    }

    @Async(value = "taskliuzeExecutor")
    public Future<Object> deleteuab(int j, List<String> ids) {
        log.info("循环次数:{},删除开始线程号:{}", j, Thread.currentThread().getName());
        // for (String id : ids) {
        try {
            appUserMapper.DELETEUseraAppBakById(ids);
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
        // }
        log.info("循环次数:{},删除结束线程号:{}", j, Thread.currentThread().getName());
        return new AsyncResult<>(null);
    }
}
